let pageSize = $('#pageSize').val();
let currentPage = $('#currentPage').text();
let total = 0;
let pages = 0;

// 新增学生
$('#addStudentsBtn').click(function () {
    const name = $('#addStudentsName').val();
    const age = $('#addStudentsAge').val();
    const gender = $('[name=addStudentsGender]:checked').val();
    //专业_id
    const subjectsId = $('#studentsSubject').val();
    //班级
    const classesId = $('#ban1').val();
    console.log(subjectsId, classesId);
    $.ajax({
        url: '/students/createStudents',
        type: 'POST',
        data: {
            name, age, gender, subjectsId, classesId
        },
        success(res) {
            if(res.code) {
                showStudents();
                alert('新增成功')
            }
        }
    })
})

//下一页
$('#nextPage').click(function (){
    currentPage++;
    showStudents({ pageSize, currentPage });//分页获取数据
})
$('#prevPage').click(function (){
    currentPage--;
    showStudents({ pageSize, currentPage });//分页获取数据
})
//尾页
$('#lastPage').click(function(){
    currentPage = pages;
    showStudents({ pageSize, currentPage });
})

//页面刷新进入时渲染数据
showStudents({ pageSize, currentPage });

//学生列表初始化
async function showStudents({pageSize, currentPage } ={}){
    const studentsData = await getStudentsData({pageSize, currentPage});
    console.log(studentsData);
    studentsRender(studentsData);
}

// 获取学生数据
function getStudentsData({ pageSize, currentPage} = {}) {
    return new Promise((resolve, reject) => {
    $.ajax({
        url: '/students/getStudents',
        type: 'GET',
        data:{
            //每页显示的条数
            pageSize,
            //当前页码
            currentPage
        },
        success(res) {
            if (res.code) {
                total = res.result.total;
                pages = res.result.pages;
                resolve(res.result.rows);
            }
        }
    })
    })
}
//渲染学生数据
function studentsRender(data){
    const studentsHtml = data.map(item => {
        return `
            <tr>
                <td>${item._id}</td>
                <td>${item.name}</td>
                <td>${item.age}</td>
                <td>${item.gender}</td>
                <td>${item.classesId ? item.classesId.name : '暂无'}</td>
                <td>${item.subjectsId ? item.subjectsId.name : '暂无'}</td>
                <td></td>
                <td>
                    <a href="#">修改</a>
                    <a href="#" class="deleteOne" data-id="${item._id}">删除</a>
                </td>
            </tr>
                        `
                }).join('');
                $('#studentsTb').html(studentsHtml);
                $('#currentPage').text(currentPage);
}

$('#studentsSubject').change(function(){
    //获取到选中的专业_id
    const subjectsId = $(this).val();
    console.log(subjectsId);
    getClassesBySubjectsId(subjectsId);
})

//学生删除
$('#studentsTb').on('click', '.deleteOne', function(e){
    e.preventDefault();
    const _id = $(this).data('id');
    $.ajax({
        url: '/students/deleteStudents',
        type:'POST',
        data:{ _id },
        success(res) {
            console.log('删除结果',res);
            if(res.code){
                showStudents();
            }
        }
    })
})